//
//  QDDoctorMassegeController.m
//  QuickDoctor
//
//  Created by ycw on 2016/12/22.
//  Copyright © 2016年 Eric. All rights reserved.
//

#import "QDDoctorMassegeController.h"
#import "QDFocusCell.h"
#import "QDFocusModel.h"

@interface QDDoctorMassegeController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>

@property(weak,nonatomic) UIBarButtonItem *rightButton;

@property(weak,nonatomic) UITableView *tableView;

@property(strong,nonatomic) QDFocusCell *focusCell;

@property(weak,nonatomic) UIScrollView *scrollView;

@property(weak,nonatomic) UITableView *rightTableView;

@property(weak,nonatomic) UIButton *btnLeft;

@property(weak,nonatomic) UIButton *btnRight;

@property(weak,nonatomic) UIView *bottomView;

///当前滚动视图显示到哪一页
@property (nonatomic, assign) NSInteger currentIndex;

@property(weak,nonatomic) UIButton *currentButton;

@end

@implementation QDDoctorMassegeController

-(void)setModel:(QDFocusModel *)model
{
    _model = model;
   
}

//重写currentIndex
- (void)setCurrentIndex:(NSInteger)currentIndex
{
   
    _currentIndex = currentIndex;
    
    //self.btnLeft.selected = currentIndex ? NO : YES;
    
    //self.btnRight.selected = currentIndex ? YES : NO;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"医生基本信息";
    
    [self setRightButton];
    
    [self createScrollView];
    
    [self createHeaderView];
    
    [self createBottomView];
    
    self.lblName.text = self.model.doctor_name;
    
    self.lblTitle.text = self.model.doctor_title_name;
    
    self.lblText.text = self.model.hospital_name;
}

#pragma mark
#pragma mark - 实现联动
- (void)createScrollView
{
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 270, self.view.bounds.size.width,self.view.bounds.size.height - 320)];
    
    [self.view addSubview:scrollView];
    
    self.scrollView = scrollView;
    
    scrollView.pagingEnabled = YES;
    
    scrollView.bounces = NO;
    
    scrollView.showsVerticalScrollIndicator = NO;
    
    scrollView.showsHorizontalScrollIndicator = NO;
    
    scrollView.delegate = self;
    
    scrollView.contentSize = CGSizeMake(self.view.bounds.size.width * 2, self.view.bounds.size.height - 250);
    
    //动态添加tableView
    
        UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];

        self.tableView = tableView;
        
        tableView.delegate = self;
        
        tableView.dataSource = self;
        
        [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        
        [self.scrollView addSubview:tableView];
    
    UITableView *rightTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
    
    self.rightTableView = rightTableView;
    
    rightTableView.delegate = self;
    
    rightTableView.dataSource = self;
    
    [rightTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"right"];
    
    [self.scrollView addSubview:rightTableView];
    
    
    
}



#pragma mark
#pragma mark - 实现数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (self.tableView == tableView) {
        return 9;
    }else{
        
        return 5;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (self.tableView == tableView)
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        
        cell.textLabel.text = [NSString stringWithFormat:@"就诊条件 :%ld行",(long)indexPath.row];
        return cell;
    }
    else
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"right" forIndexPath:indexPath];
        
        cell.textLabel.text = [NSString stringWithFormat:@"医生简介 :%ld行",(long)indexPath.row];
        
        return cell;
    }
    
}


#pragma mark
#pragma mark - 创建右侧button
- (UIBarButtonItem *)setRightButton
{
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"kongxin"] style:UIBarButtonItemStylePlain target:self action:@selector(clickStar)];
    
    self.rightButton = rightButton;
    
    self.navigationItem.rightBarButtonItem = rightButton;
    
    return rightButton;
    
}
#pragma mark
#pragma mark - 点击星星
- (void) clickStar
{
    
    if ([self.rightButton.image isEqual:[UIImage imageNamed:@"kongxin"]]) {
        
        [self.rightButton setImage:[UIImage imageNamed:@"shixin"]];
        
    }else{
        
        [self.rightButton setImage:[UIImage imageNamed:@"kongxin"]];
        
    }
    
}

#pragma mark
#pragma mark - 设置表头

- (void)createHeaderView
{
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,64, [UIScreen mainScreen].bounds.size.width, 200)];
    
    [self.view addSubview:headerView];
    
    //headerView.backgroundColor = [UIColor redColor];
    
    UIImageView *imgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"illness_img_person.png"]];
    
    imgV.layer.masksToBounds = YES;
    
    imgV.layer.cornerRadius = 50;
    
    [headerView addSubview:imgV];
    
    [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(headerView).mas_offset(20);
            
            make.leading.equalTo(headerView).mas_offset(20);
            
            make.width.mas_equalTo(100);
            
            make.height.mas_equalTo(100);
        
    }];
    
    UILabel *lblName = [[UILabel alloc] initWithFrame:CGRectMake(140, 20, 60, 30)];
    
    lblName.font = [UIFont systemFontOfSize:18];
    
    //lblName.backgroundColor = [UIColor redColor];
    
    //lblName.text = _focusCell.lblName.text;
    
    //lblName.text = @"ewrw";
    
    [headerView addSubview:lblName];
    
    self.lblName = lblName;
    
    
    UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(200, 20, 100, 30)];
    
    lblTitle.font = [UIFont systemFontOfSize:15];
    
    lblTitle.textColor = [UIColor lightGrayColor];
    
    [headerView addSubview:lblTitle];
    
    self.lblTitle = lblTitle;
    
    //lblTitle.text = @"haha";
    
    UILabel *lblText = [[UILabel alloc] initWithFrame:CGRectMake(140, 60, 250, 30)];
    
    lblText.font = [UIFont systemFontOfSize:15];
    
    lblText.textColor = [UIColor lightGrayColor];
    
    [headerView addSubview:lblText];
    
    self.lblText = lblText;
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [btn1 setImage:[UIImage imageNamed:@"yuyueliang"] forState:UIControlStateNormal];
    
    [btn1 setTitle:@"35" forState:UIControlStateNormal];
    
    btn1.titleLabel.font = [UIFont systemFontOfSize:12];
    
    [btn1 setTitleColor:[UIColor colorWithRed:57/255.0 green:197/255.0 blue:196/255.0 alpha:1] forState:UIControlStateNormal];
    
    [btn1 setTitleEdgeInsets:UIEdgeInsetsMake(15, -btn1.imageView.bounds.size.width + 10, 15, 0)];
    [btn1 setImageEdgeInsets:UIEdgeInsetsMake(10, 5, 10, 0)];
    
    [headerView addSubview:btn1];
    
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(lblName);
        
        make.top.equalTo(lblText.mas_bottom).mas_offset(5);
        
        make.height.mas_equalTo(40);
        
        make.width.mas_equalTo(40);
        
    }];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [btn2 setImage:[UIImage imageNamed:@"xianhua"] forState:UIControlStateNormal];
    
    [btn2 setTitle:@"194" forState:UIControlStateNormal];
    
    btn2.titleLabel.font = [UIFont systemFontOfSize:12];
    
    [btn2 setTitleColor:[UIColor colorWithRed:57/255.0 green:197/255.0 blue:196/255.0 alpha:1] forState:UIControlStateNormal];
    
    [btn2 setTitleEdgeInsets:UIEdgeInsetsMake(15, -btn2.imageView.bounds.size.width + 10, 15, 0)];
    [btn2 setImageEdgeInsets:UIEdgeInsetsMake(10, 5, 10, 0)];
    
    [headerView addSubview:btn2];
    
    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(btn1.mas_trailing).mas_offset(5);
        
        make.top.equalTo(lblText.mas_bottom).mas_offset(5);
        
        make.height.mas_equalTo(40);
        
        make.width.mas_equalTo(50);
        
    }];
    
    UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [btn3 setImage:[UIImage imageNamed:@"jinqi"] forState:UIControlStateNormal];
    
    [btn3 setTitle:@"100" forState:UIControlStateNormal];
    
    btn3.titleLabel.font = [UIFont systemFontOfSize:12];
    
    [btn3 setTitleColor:[UIColor colorWithRed:57/255.0 green:197/255.0 blue:196/255.0 alpha:1] forState:UIControlStateNormal];
    
    [btn3 setTitleEdgeInsets:UIEdgeInsetsMake(15, -btn3.imageView.bounds.size.width + 10, 15, 0)];
    [btn3 setImageEdgeInsets:UIEdgeInsetsMake(10, 5, 10, 0)];
    
    [headerView addSubview:btn3];
    
    [btn3 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(btn2.mas_trailing).mas_offset(5);
        
        make.top.equalTo(lblText.mas_bottom).mas_offset(5);
        
        make.height.mas_equalTo(40);
        
        make.width.mas_equalTo(50);
        
    }];
    
    UIView *honriView = [[UIView alloc] initWithFrame:CGRectMake(0, 150, self.view.bounds.size.width, 5)];
    
    honriView.backgroundColor = [UIColor colorWithRed:224/255.0 green:225/255.0 blue:226/255.0 alpha:1];
    
    [headerView addSubview:honriView];
    
    UIButton *btnLeft = [[UIButton alloc] initWithFrame:CGRectMake(0, 160, self.view.bounds.size.width / 2 - 10, 30)];
    
    self.btnLeft = btnLeft;
    
    [btnLeft setTitle:@"就诊条件" forState:UIControlStateNormal];
    
    [btnLeft setTitleColor:[UIColor colorWithRed:32/255.0 green:198/255.0 blue:198/255.0 alpha:1] forState:UIControlStateNormal];
    
    //btnLeft.backgroundColor = [UIColor redColor];
    
    [headerView addSubview:btnLeft];
    
    //初始化button的点击
    btnLeft.selected = YES;
    
    [btnLeft addTarget:self action:@selector(clickLeftButton) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *middleView = [[UIView alloc] initWithFrame:CGRectMake(190, 165, 1, 30)];
    
    middleView.backgroundColor = [UIColor colorWithRed:224/255.0 green:225/255.0 blue:226/255.0 alpha:1];
    
    [headerView addSubview:middleView];
    
    UIButton *btnRight = [[UIButton alloc] initWithFrame:CGRectMake(200, 160, self.view.bounds.size.width / 2 - 10, 30)];
    
    self.btnRight = btnRight;
    
    [btnRight setTitle:@"医生简介" forState:UIControlStateNormal];
    
    [btnRight setTitleColor:[UIColor colorWithRed:224/255.0 green:225/255.0 blue:226/255.0 alpha:1] forState:UIControlStateNormal];
    
    //btnLeft.backgroundColor = [UIColor redColor];
    
    [headerView addSubview:btnRight];
    
    [btnRight addTarget:self action:@selector(clickRightButton) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *bottomView = [[UIView alloc] init];
    //WithFrame:CGRectMake(30, 195, 100, 2)
    bottomView.backgroundColor = [UIColor colorWithRed:32/255.0 green:198/255.0 blue:198/255.0 alpha:1];
    
    [headerView addSubview:bottomView];
    
    self.bottomView = bottomView;
    
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(headerView).mas_offset(40);
        
        make.top.equalTo(headerView).mas_offset(195);
        
        make.height.mas_offset(2);
        
        make.width.mas_offset(100);
        
    }];
    
}

#pragma mark
#pragma mark - 实现点击右边button的监听方法
- (void) clickRightButton
{
    
    
    [self.scrollView setContentOffset:CGPointMake(self.view.bounds.size.width, 0) animated:YES];
    
//    // 约束动画
    [UIView animateWithDuration:.25 animations:^{
        
        [self.view layoutIfNeeded];
    }];
    
    
}

#pragma mark
#pragma mark - 实现点击左边button的监听方法
- (void) clickLeftButton
{
    
    [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    
    
    [UIView animateWithDuration:.25 animations:^{
        
        [self.view layoutIfNeeded];
    }];
    
}

#pragma mark
#pragma mark - scrollView的代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offset = scrollView.contentOffset.x/2;

    [self.bottomView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(_btnLeft).mas_offset(offset);
        
        make.height.mas_offset(2);
        
        make.width.mas_offset(100);
        
        make.top.equalTo(_btnLeft.mas_bottom).mas_offset(5);
        
    }];
    
    //约束动画
    [UIView animateWithDuration:.25 animations:^{
        
        [self.view layoutIfNeeded];
    }];
    if (offset >= 100) {
        
        [self.btnRight setTitleColor:[UIColor colorWithRed:32/255.0 green:198/255.0 blue:198/255.0 alpha:1] forState:UIControlStateNormal];
        
        [self.btnLeft setTitleColor:[UIColor colorWithRed:224/255.0 green:225/255.0 blue:226/255.0 alpha:1] forState:UIControlStateNormal];
        
    }else if((ABS(offset) <= 100) ){
        
        [self.btnLeft setTitleColor:[UIColor colorWithRed:32/255.0 green:198/255.0 blue:198/255.0 alpha:1] forState:UIControlStateNormal];
        
         [self.btnRight setTitleColor:[UIColor colorWithRed:224/255.0 green:225/255.0 blue:226/255.0 alpha:1] forState:UIControlStateNormal];
        
    }
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat offset = scrollView.contentOffset.x;
    self.currentIndex = offset / self.view.bounds.size.width;
    
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    CGFloat offset = scrollView.contentOffset.x;
    self.currentIndex = offset / self.view.bounds.size.width;
    
}
#pragma mark
#pragma mark -  创建底部视图
- (void) createBottomView
{
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 620, self.view.bounds.size.width, 50)];
    
    //footerView.backgroundColor = [UIColor redColor];
    
    [self.view addSubview:footerView];
    
    UIButton *footerButton = [[UIButton alloc] init];
    
    [footerButton setTitle:@"咨询医生" forState:UIControlStateNormal];
    
    [footerButton setBackgroundColor:[UIColor colorWithRed:32/255.0 green:198/255.0 blue:198/255.0 alpha:1]];
    
    [footerView addSubview:footerButton];
    
    [footerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(footerView);
        
    }];
    
    [footerButton addTarget:self action:@selector(clickFooterButton) forControlEvents:UIControlEventTouchUpInside];
    
    
    
}


#pragma mark
#pragma mark - 点击咨询医生
- (void) clickFooterButton
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


@end
